﻿Imports System.Xml.Linq
Imports System.Windows.Forms
Imports System.Exception

''' <summary>
''' The main module for XMLmosaic.
''' </summary>
''' <remarks></remarks>
Module Module1

    ''' <summary>
    ''' The address for the current XMLmosaic Solution.
    ''' </summary>
    ''' <remarks></remarks>
    Public SolAdd As String = ""

    ''' <summary>
    ''' The main procedure for the Interpreter which controls the main execution.
    ''' </summary>
    ''' <remarks></remarks>
    Sub Main()
        Console.WriteLine("Welcome to the Xmlmosaic Interpreter version:" & My.Application.Info.Version.ToString())
        LoadFile()
        Console.ReadKey()
    End Sub

    ''' <summary>
    ''' Loads the solution file for the program that is to be executed.
    ''' </summary>
    ''' <remarks></remarks>
    Sub LoadFile()
        Try
            Console.WriteLine("Enter the address of the solution xml file")
            Dim filePath As String = Console.ReadLine()

            SolAdd = filePath

            Dim solution As XElement = XElement.Load(SolAdd)
            Dim StartClass As XElement = <Root>
                                             <%= From el In solution.<StartClass> _
                                                 Select el %>
                                         </Root>
            Dim startstr As String = StartClass
            Dim program As XElement = XElement.Load(startstr)

            Dim MainCode As XElement = _
                                <Root>
                                    <%= From el In program.<Method>.<Code> _
                                        Where el.Attribute("id") = "1" _
                                        Select el %>
                                </Root>

            Dim Vars As XElement = <Variables>
                                       <Type>XMLmosaic Variables</Type>
                                       <Counter>
                                           <Count>0</Count>
                                       </Counter>
                                   </Variables>

            If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO") = False Then
                My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO")
            End If

            My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml", Vars.ToString, False)

            Dim MainStr As String = MainCode
            Run(MainStr)

        Catch ex As Exception
            MsgBox("Error Processing Source Code")
            MsgBox(ex.ToString)
        End Try
    End Sub

    ''' <summary>
    ''' Prepares a method or function for execution then executes it.
    ''' </summary>
    ''' <param name="Code">The code for a method or function as it is in the XMLmosaic class.</param>
    ''' <remarks></remarks>
    Sub Run(ByVal Code As String)

        Dim words As String = Code
        Dim split As String() = words.Split(New [Char]() {"{"c, "}"c})


        Dim words2 As String = split(1)
        Dim split2 As String() = words2.Split(New [Char]() {CChar(vbLf)})

        Dim output As String = ""
        Dim s As String

        For Each s In split2
            output = output & s
        Next

        Exec(output)
    End Sub

    ''' <summary>
    ''' Exeutes a set of instructions that are supplied by Module1.Run
    ''' </summary>
    ''' <param name="Instructions">The instructions to execute.</param>
    ''' <remarks></remarks>
    Sub Exec(ByVal Instructions As String)

        Dim words As String = Instructions
        Dim len As Integer = words.Length - 1
        Dim ins As Boolean = False
        Dim i As Integer = -1

        Dim ch As String = ""

        Dim s As String = ""

        Do Until i = len
            i = i + 1

            ch = words.Chars(i)

            If ch = "'" Then
                If ins = False Then
                    ins = True
                ElseIf ins = True Then
                    ins = False
                End If
            End If

            If ins = False Then
                If ch <> ";" Then
                    s = s & ch
                End If
            ElseIf ins = True Then
                s = s & ch
            End If

            If ch = ";" Then
                If ins = False Then
                    Executor.Process(s)
                    s = ""
                End If
            End If

        Loop


    End Sub

End Module